package com.hq.ims.userpc.busservice.app;

import com.google.common.collect.Lists;
import com.hq.ims.data.entity.*;
import com.hq.ims.data.event.BusinfoFeedbackEvent;
import com.hq.ims.data.service.*;
import com.hq.ims.data.utils.LocalConstant;
import com.hq.ims.data.vo.AppFileMapstruct;
import com.hq.ims.data.vo.BusInfoFeedbackMapstruct;
import com.hq.ims.data.vo.BusInfoNotificationMapstruct;
import com.hq.ims.data.vo.BusiInfoMapstruct;
import com.hq.ims.userpc.busservice.UserBusiInfoService;
import com.hq.utils.util.ListUtils;
import com.hq.utils.vo.ReturnVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author cx
 * @date 2024/11/26 11:21:02
 * @description 经营信息跟踪反馈Service
 */
@Service
public class AppBusInfoTrackService {

    @Resource
    private BaseBusInfoNotificationService baseBusInfoNotificationService;
    @Resource
    private BaseBusInfoTrackService baseBusInfoTrackService;
    @Resource
    private UserBusiInfoService busiInfoService;
    @Resource
    private BaseBusiInfoService baseBusiInfoService;
    @Resource
    private BaseBusInfoFeedbackService baseBusInfoFeedbackService;
    @Resource
    private BaseBusInfoFeedbackDeriveService baseBusInfoFeedbackDeriveService;
    @Resource
    private ApplicationContext applicationContext;
    @Resource
    private BaseFileRecordService baseFileRecordService;

    /**
     * 反馈填写
     *
     * @param saveVo 反馈vo
     * @return ReturnVo
     */
    @Transactional(rollbackFor = Exception.class)
    public ReturnVo<Boolean> fillFeedback(BusInfoFeedbackMapstruct.SaveVo saveVo) {
        // 添加反馈数据
        BusInfoFeedback busInfoFeedback = baseBusInfoFeedbackService.addData(saveVo);
        // 判断反馈时，反馈内容是否有文件
        List<AppFileMapstruct.FileVo> fileVoList = saveVo.getFileVoList();
        if (ListUtils.isExistsEle(fileVoList)) {
            //保存文件
            baseFileRecordService.saveFile(fileVoList, BusInfoFeedback.class, busInfoFeedback.getBusInfoFeedbackId(),
                    LocalConstant.FILE_FEEDBACK_CONTENT, true);
        }
        //判断是否有派生信息
        if (CollectionUtils.isNotEmpty(saveVo.getDeriveBusiInfoList())) {
            List<BusInfoFeedbackDerive> busInfoFeedbackDeriveList = Lists.newArrayList();
            List<BusiInfoMapstruct.SaveVo> deriveBusiInfoList = saveVo.getDeriveBusiInfoList();
            deriveBusiInfoList.forEach(deriveBusiInfo -> {
                BusiInfo busiInfo = baseBusiInfoService.getById(busInfoFeedback.getBusiInfoId());
                busiInfo.setParentId(busInfoFeedback.getBusiInfoId());
                BeanUtils.copyProperties(busiInfo, deriveBusiInfo);
                // 添加派生经营信息
                String busiInfoId = busiInfoService.addBusiInfo(deriveBusiInfo);
                BusInfoFeedbackDerive busInfoFeedbackDerive = new BusInfoFeedbackDerive();
                busInfoFeedbackDerive.setBusInfoFeedbackId(busInfoFeedback.getBusInfoFeedbackId());
                busInfoFeedbackDerive.setBusiInfoId(busInfoFeedback.getBusiInfoId());
                busInfoFeedbackDerive.setDeriveBusiInfoId(busiInfoId);
                busInfoFeedbackDeriveList.add(busInfoFeedbackDerive);
            });
            // 添加经营信息反馈派生数据
            baseBusInfoFeedbackDeriveService.saveBatch(busInfoFeedbackDeriveList);
        }
        // 更新跟踪表中的最新的反馈阶段, 反馈时间, 反馈ID
        BusInfoTrack busInfoTrack = baseBusInfoTrackService.getById(saveVo.getBusInfoTrackId());
        busInfoTrack.setLastNewFeedbackPhase(busInfoFeedback.getFeedbackPhase());
        busInfoTrack.setLastNewFeedbackDate(busInfoFeedback.getFeedbackDate());
        busInfoTrack.setLastNewBusInfoFeedbackId(busInfoFeedback.getBusInfoFeedbackId());
        baseBusInfoTrackService.updateById(busInfoTrack);

        //发布事件
        applicationContext.publishEvent(new BusinfoFeedbackEvent(this, busInfoFeedback, BusinfoFeedbackEvent.ADD_EVENT));
        //返回值
        return ReturnVo.buildSuccess(Boolean.TRUE);
    }

    /**
     * 反馈情况查询
     *
     * @param busiInfoId 经营信息id
     * @return ReturnVo
     */
    public ReturnVo<List<BusInfoFeedbackMapstruct.FeedbackDetailVo>> getFeedbackSituation(String busiInfoId) {
        // 反馈情况列表
        List<BusInfoFeedback> busInfoFeedbackList = baseBusInfoFeedbackService.lambdaQuery()
                .eq(BusInfoFeedback::getBusiInfoId, busiInfoId)
                .orderByDesc(BusInfoFeedback::getFeedbackDate)
                .list();
        List<BusInfoFeedbackMapstruct.FeedbackDetailVo> feedbackDetailVoList = BusInfoFeedbackMapstruct.INSTANCE.toFeedbackDetailVoList(busInfoFeedbackList);
        feedbackDetailVoList.forEach(busInfoFeedbackVo -> {
            // 派生的经营信息数量
            Integer deriveBusiInfoCount = busInfoFeedbackVo.getDeriveBusiInfoCount();
            if (deriveBusiInfoCount > 0) {
                // 查询派生的经营信息列表
                List<BusInfoFeedbackMapstruct.DeriveBusiInfoVo> deriveBusiInfoList =
                        baseBusInfoFeedbackDeriveService.getDeriveBusiInfoList(busInfoFeedbackVo.getBusInfoFeedbackId());
                busInfoFeedbackVo.setDeriveBusiInfoList(deriveBusiInfoList);
            }
        });
        return ReturnVo.buildSuccess(feedbackDetailVoList);
    }

    /**
     * 查询接收情况
     *
     * @param busiInfoId 经营信息id
     * @return ReturnVo
     */
    public ReturnVo<List<BusInfoNotificationMapstruct.ReceiveSituationVo>> getReceiveSituation(String busiInfoId) {
        List<BusInfoNotification> busInfoNotificationList = baseBusInfoNotificationService.lambdaQuery()
                .eq(BusInfoNotification::getBusiInfoId, busiInfoId)
                .list();
        List<BusInfoNotificationMapstruct.ReceiveSituationVo> receiveSituationVoList =
                BusInfoNotificationMapstruct.INSTANCE.toReceiveSituationVoList(busInfoNotificationList);
        return ReturnVo.buildSuccess(receiveSituationVoList);
    }
}
